// pages/search/search.js
import http from '../../utils/http'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        historylist: [],
        inputValue: '',
        mytext: '',
        goodslist: [],
        categorieslist: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.firstRequest()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    handleclear() {
        http({
            url: "http://localhost:3000/history?id=1",
            method: "put",
            data: {
                content: [],
                goodslist: [],
            }
        })
        this.setData({
            historylist: []
        })
    },
    handleInput(evt) {
        if (evt.detail.value.length == 0) {
            this.setData({
                goodslist:[],
                categorieslist:[],
                inputValue:'',
                mytext:''
            })
            return
        }
        this.setData({
            inputValue: evt.detail.value,
            mytext: evt.detail.value
        })

    },
    handleSearch(evt) {
        if (this.data.inputValue.length === 0) {
            this.setData({
                goodslist:[],
                categorieslist:[],
                inputValue:'',
                mytext:''
            })
            return
        }
        var historylistArr = this.data.historylist
        historylistArr.unshift({
            content: this.data.inputValue
        })
        this.searchajax()
        http({
            url: "http://localhost:3000/history?id=1",
            method: "PUT",
            data: {
                content: historylistArr
            }
        }).then(res => {
            this.setData({
                historylist: res.data.content,
            })
        })
    },
    handleDelete(){
        this.setData({
            inputValue: '',
            mytext: '',
            goodslist:[],
            categorieslist:[]
        })
    },
    handleCategories(evt){
        wx.navigateTo({
            url: `/pages/searchlist/searchlist?id=${evt.currentTarget.dataset.id}&title=${evt.currentTarget.dataset.title}`,
          })
    },
    handleGoods(evt){
        wx.navigateTo({
            url: `/pages/detail/detail?id=${evt.currentTarget.dataset.id}&title=${evt.currentTarget.dataset.title}`,
          })
    },
    handlehistoryItem(evt){
        this.setData({
            inputValue:evt.currentTarget.dataset.content,
            mytext:evt.currentTarget.dataset.content
        })
    },
    firstRequest() {
        http({
            url: "http://localhost:3000/history",
            method: "GET",
        }).then(res => {
            this.setData({
                historylist: res.data.content
            })
        })
    },
    searchajax() {
        Promise.all([
            http({
                url: `http://localhost:3000/categories?title_like=${this.data.inputValue}`,
                method: "get"
            }),
            http({
                url: `http://localhost:3000/goods?title_like=${this.data.inputValue}`,
                method: "get"
            })
        ]).then(res => {
            this.setData({
                categorieslist: res[0].data,
                goodslist: res[1].data
            })
        })
    }
})